/**
 *
 */
package com.ce.pms.house.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.util.UpLoadFile;
import com.ce.pms.customer.dao.IParticipantDao;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IAreaDao;
import com.ce.pms.house.dao.ICoWorkerDao;
import com.ce.pms.house.dao.IImageDao;
import com.ce.pms.house.dao.IProjectDao;
import com.ce.pms.house.model.AreaDomain;
import com.ce.pms.house.model.CoWorkerDomain;
import com.ce.pms.house.model.Environment;
import com.ce.pms.house.model.ImageDomain;
import com.ce.pms.house.model.Layout;
import com.ce.pms.house.model.ProjectDomain;
import com.ce.pms.house.service.ICoWorkerService;
import com.ce.pms.house.service.IImageService;
import com.ce.pms.house.service.IProjectService;
import com.ce.pms.house.vo.CoWorkerVO;
import com.ce.pms.house.vo.EnvironmentVO;
import com.ce.pms.house.vo.ImageVO;
import com.ce.pms.house.vo.LayoutVO;
import com.ce.pms.house.vo.ProjectVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 功能描述: ProjectServiceImpl 期数维护
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 付湘辉 Created on： 2008-6-25
 * @author update on：
 */

public class ProjectServiceImpl extends PmsBaseServiceImpl implements
		IProjectService {

	/**
	 * @roseuid 4861E76F0213
	 */
	public ProjectServiceImpl() {

	}

	/**
	 * 方法描述: 1. 根据projectId查询ProjectDomain实例 2.
	 * 根据具体业务,设置/替换/覆盖ProjectDomain相关属性.
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param o
	 * @return 返回当前页的查询结果
	 * @roseuid 48479B8B03A9 Edit on: 修改原因：
	 */
	private ProjectDomain assembleV2D(ProjectVO o) {

		ProjectDomain projectDO = new ProjectDomain();
		if (BlankUtil.isBlank(o)) {
			return projectDO;
		}
		AreaDomain areaDomain = areaDao.findById(o.getAreaId() == null ? null
				: o.getAreaId());
		projectDO.setArea(areaDomain);
		// projectDO.setLayout(this.assembleV2D(o.getLayoutVO()));
		projectDO.setName(o.getName());
		projectDO.setKeyword(o.getKeyword());
		SysInfo sysInfo = new SysInfo();
		sysInfo = createInfo(o.getProjectCreator());
		projectDO.setSysInfo(sysInfo);
		return projectDO;
	}

	/**
	 * 方法描述: 根据environment装配EnvironmentVO
	 *
	 * @author 胡煦 Create on:2008-6-27
	 * @param o
	 * @return ProjectVO Edit author:
	 * @roseuid 48479B9B0119 修改原因：
	 */

	public EnvironmentVO assembleD2V(Environment environment) {
		EnvironmentVO environmentVO = new EnvironmentVO();

		if (BlankUtil.isBlank(environment)) {
			return environmentVO;
		}
		PopulateUtil.populate(environment, environmentVO);
		environmentVO.setProjectId(environment.getProject() == null ? null
				: environment.getProject().getKeyword());

		if (BlankUtil.isBlank(environment.getIchnographysImage())) {
			environmentVO.setIchnographysImage(null);
		}
		ImageDomain imageDomain1 = environment.getIchnographysImage();
		environmentVO.setIchnographysImage(imageService
				.convertDomainToVo(imageDomain1));

		if (BlankUtil.isBlank(environment.getVisionImage())) {
			environmentVO.setVisionImage(null);
		}
		ImageDomain imageDomain2 = environment.getVisionImage();
		environmentVO.setVisionImage(imageService
				.convertDomainToVo(imageDomain2));

		return environmentVO;
	}

	/**
	 * 方法描述: 根据ProjectDomain装配ProjectVO
	 *
	 * @author 胡煦 Create on:2008-6-27
	 * @param o
	 * @return ProjectVO Edit author:
	 * @roseuid 48479B9B0119 修改原因：
	 */

	public ProjectVO assembleD2V(ProjectDomain o) {
		ProjectVO projectVO = new ProjectVO();
		if (BlankUtil.isBlank(o)) {
			return projectVO;
		}

		projectVO.setAreaId(o.getArea().getKeyword());
		LayoutVO layoutVO = assembleD2V(o.getLayout());
		layoutVO.setProjectId(o.getKeyword());
		projectVO.setLayoutVO(layoutVO);
		projectVO.setName(o.getName());
		projectVO.setKeyword(o.getKeyword());

		Map<Long, CoWorkerVO> buildCorpsmap = new HashMap<Long, CoWorkerVO>();
		for (CoWorkerDomain coWorkerDomain : o.getBuildCorps()) {
			buildCorpsmap.put(coWorkerDomain.getParticipantRoleId(),
					coWorkerService
							.getCoWorkerVOByCoWorkerDomain(coWorkerDomain));
			projectVO.setBuildCorps(buildCorpsmap);
		}
		Map<Long, CoWorkerVO> designCorpsmap = new HashMap<Long, CoWorkerVO>();
		for (CoWorkerDomain coWorkerDomain : o.getDesignCorps()) {
			designCorpsmap.put(coWorkerDomain.getParticipantRoleId(),
					coWorkerService
							.getCoWorkerVOByCoWorkerDomain(coWorkerDomain));
			projectVO.setDesignCorps(designCorpsmap);
		}
		Map<Long, CoWorkerVO> developersmap = new HashMap<Long, CoWorkerVO>();
		for (CoWorkerDomain coWorkerDomain : o.getDevelopers()) {
			developersmap.put(coWorkerDomain.getParticipantRoleId(),
					coWorkerService
							.getCoWorkerVOByCoWorkerDomain(coWorkerDomain));
			projectVO.setDeveloper(developersmap);
		}
		Map<Long, CoWorkerVO> surveillantsmap = new HashMap<Long, CoWorkerVO>();
		for (CoWorkerDomain coWorkerDomain : o.getSurveillants()) {
			surveillantsmap.put(coWorkerDomain.getParticipantRoleId(),
					coWorkerService
							.getCoWorkerVOByCoWorkerDomain(coWorkerDomain));
			projectVO.setSurveillants(surveillantsmap);
		}
		// projectVO.setSizeTotal(o.getLayout() == null ? null : o.getLayout()
		// .getSizeTotal().doubleValue());
		// projectVO.setSizeUsed(o.getLayout() == null ? null : o.getLayout()
		// .getSizeUsed().doubleValue());

		List<EnvironmentVO> list = new ArrayList();
		for (Environment environment : o.getEnvironments()) {
			list.add(assembleD2V(environment));
		}

		projectVO.setEnvironmentVO(list);
		projectVO.setProjectCreator(o.getSysInfo() == null ? null : o
				.getSysInfo().getCreator());
		projectVO.setProjectModifier(o.getSysInfo() == null ? null : o
				.getSysInfo().getModifier());
		projectVO.setProjectModifyDate(DateUtil.convertDateToStr((Timestamp) (o
				.getSysInfo() == null ? null : o.getSysInfo().getModifyDate()),
				"yyyy-MM-dd"));
		return projectVO;
	}

	/**
	 * 算法描述: 1. call 图片保存子流
	 *
	 * @param name
	 * @param path
	 * @roseuid 48479C690242
	 */
	private void saveOrUpdateImage(String name, String path) {

	}

	/**
	 * 方法描述: Vo to Do
	 *
	 * @author 胡煦 Create on:2008-6-26
	 * @param LayoutVO
	 * @return Layout Edit author: Edit on: 修改原因：
	 */

	public Environment assembleV2D(EnvironmentVO environmentVO) {

		Environment environment = new Environment();
		if (BlankUtil.isBlank(environmentVO)) {
			return environment;
		}
		environment.setName(environmentVO.getName());
		environment.setKeyword(environmentVO.getKeyword());
		environment.setDescription(environmentVO.getDescription());
		environment.setProject(projectDao
				.findById(environmentVO.getProjectId()));
		SysInfo sysInfo = createInfo(environmentVO.getCreator());
		environment.setSysInfo(sysInfo);

		return environment;
	}

	/**
	 * 方法描述: Vo to Do
	 *
	 * @author 胡煦 Create on:2008-6-26
	 * @param LayoutVO
	 * @return Layout Edit author: Edit on: 修改原因：
	 */
	public Layout assembleV2D(LayoutVO layoutVO) {

		Layout Layout = new Layout();
		if (BlankUtil.isBlank(layoutVO)) {
			return Layout;
		}
		Layout.setBldgPercent(layoutVO.getBldgPercent() == null ? null
				: BigDecimal.valueOf(layoutVO.getBldgPercent()));
		Layout.setDensity(layoutVO.getDensity() == null ? null : BigDecimal
				.valueOf(layoutVO.getDensity()));
		Layout.setFloorArea(layoutVO.getFloorArea() == null ? null : BigDecimal
				.valueOf(layoutVO.getFloorArea()));
		Layout.setGlebeNo(layoutVO.getGlebeNo());
		Layout.setGreenPercent(layoutVO.getGreenPercent() == null ? null
				: BigDecimal.valueOf(layoutVO.getGreenPercent()));
		Layout.setKeyword(layoutVO.getKeyword());
		Layout.setParkAmount(layoutVO.getParkAmount() == null ? null
				: BigDecimal.valueOf(layoutVO.getParkAmount()));
		Layout.setSizeBusiness(layoutVO.getSizeBusiness() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizeBusiness()));
		Layout.setSizechamber(layoutVO.getSizechamber() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizechamber()));
		Layout.setSizeCommerce(layoutVO.getSizeCommerce() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizeCommerce()));
		Layout.setSizeConstruct(layoutVO.getSizeConstruct() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizeConstruct()));
		Layout.setSizeGreen(layoutVO.getSizeGreen() == null ? null : BigDecimal
				.valueOf(layoutVO.getSizeGreen()));
		Layout.setSizegroud(layoutVO.getSizegroud() == null ? null : BigDecimal
				.valueOf(layoutVO.getSizegroud()));
		Layout.setSizeLiving(layoutVO.getSizeLiving() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizeLiving()));
		Layout.setSizeManagement(layoutVO.getSizeManagement() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizeManagement()));
		Layout.setSizeOther(layoutVO.getSizeOther() == null ? null : BigDecimal
				.valueOf(layoutVO.getSizeOther()));
		Layout.setSizePark(layoutVO.getSizePark() == null ? null : BigDecimal
				.valueOf(layoutVO.getSizePark()));
		Layout.setSizeSchool(layoutVO.getSizeSchool() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizeSchool()));
		Layout.setSizeTotal(layoutVO.getSizeTotal() == null ? null : BigDecimal
				.valueOf(layoutVO.getSizeTotal()));
		Layout.setSizeUndergroud(layoutVO.getSizeUndergroud() == null ? null
				: BigDecimal.valueOf(layoutVO.getSizeUndergroud()));
		Layout.setSizeUsed(layoutVO.getSizeUsed() == null ? null : BigDecimal
				.valueOf(layoutVO.getSizeUsed()));
		Layout.setSizeWay(layoutVO.getSizeWay() == null ? null : BigDecimal
				.valueOf(layoutVO.getSizeWay()));
		return Layout;
	}

	/**
	 * 方法描述: Do to Vo
	 *
	 * @author 胡煦 Create on:2008-6-26
	 * @param Layout
	 * @return LayoutVO Edit author: Edit on: 修改原因：
	 */
	public LayoutVO assembleD2V(Layout layout) {

		LayoutVO layoutVO = new LayoutVO();
		if (BlankUtil.isBlank(layout)) {
			return layoutVO;
		}
		layoutVO.setBldgPercent(layout.getBldgPercent() == null ? null : layout
				.getBldgPercent().doubleValue());
		layoutVO.setDensity(layout.getDensity() == null ? null : layout
				.getDensity().doubleValue());
		layoutVO.setFloorArea(layout.getFloorArea() == null ? null : layout
				.getFloorArea().doubleValue());
		layoutVO.setGlebeNo(layout.getGlebeNo());
		layoutVO.setGreenPercent(layout.getGreenPercent() == null ? null
				: layout.getGreenPercent().doubleValue());
		layoutVO.setKeyword(layout.getKeyword());
		layoutVO.setParkAmount(layout.getParkAmount() == null ? null : layout
				.getParkAmount().doubleValue());
		layoutVO.setSizeBusiness(layout.getSizeBusiness() == null ? null
				: layout.getSizeBusiness().doubleValue());
		layoutVO.setSizechamber(layout.getSizechamber() == null ? null : layout
				.getSizechamber().doubleValue());
		layoutVO.setSizeCommerce(layout.getSizeCommerce() == null ? null
				: layout.getSizeCommerce().doubleValue());
		layoutVO.setSizeConstruct(layout.getSizeConstruct() == null ? null
				: layout.getSizeConstruct().doubleValue());
		layoutVO.setSizeGreen(layout.getSizeGreen() == null ? null : layout
				.getSizeGreen().doubleValue());
		layoutVO.setSizegroud(layout.getSizegroud() == null ? null : layout
				.getSizegroud().doubleValue());
		layoutVO.setSizeLiving(layout.getSizeLiving() == null ? null : layout
				.getSizeLiving().doubleValue());
		layoutVO.setSizeManagement(layout.getSizeManagement() == null ? null
				: layout.getSizeManagement().doubleValue());
		layoutVO.setSizeOther(layout.getSizeOther() == null ? null : layout
				.getSizeOther().doubleValue());
		layoutVO.setSizePark(layout.getSizePark() == null ? null : layout
				.getSizePark().doubleValue());
		layoutVO.setSizeSchool(layout.getSizeSchool() == null ? null : layout
				.getSizeSchool().doubleValue());
		layoutVO.setSizeTotal(layout.getSizeTotal() == null ? null : layout
				.getSizeTotal().doubleValue());
		layoutVO.setSizeUndergroud(layout.getSizeUndergroud() == null ? null
				: layout.getSizeUndergroud().doubleValue());
		layoutVO.setSizeUsed(layout.getSizeUsed() == null ? null : layout
				.getSizeUsed().doubleValue());
		layoutVO.setSizeWay(layout.getSizeWay() == null ? null : layout
				.getSizeWay().doubleValue());
		return layoutVO;
	}

	/**
	 * @param o
	 * @return ProjectVO
	 * @roseuid 4861E76F0261
	 */
	public ProjectVO addBase(ProjectVO o) {

		super.checkParameterIsNull(o, "common:parameters_null_exception, ");
		ProjectDomain pda = assembleV2D(o);
		pda = projectDao.saveObject(pda);
		return assembleD2V(pda);
	}

	/**
	 * @param o
	 * @return ProjectVO
	 * @roseuid 4861E76F02CE
	 */
	public ProjectVO editBase(ProjectVO o) {

		super.checkParameterIsNull(o, "common:parameters_null_exception, ");

		if (checkProjectName(o.getName(), null) > 0) {
			throw new BusinessException("house:project_name_exist_exception",
					BusinessException.WARNING);
		}

		ProjectDomain pda = projectDao.findById(o.getKeyword());
		modifyInfo(pda.getSysInfo(), o.getProjectModifier());
		pda.setName(o.getName());
		pda = projectDao.updateObject(pda);
		return assembleD2V(pda);
	}

	/**
	 * 方法描述: 新增期数的建设规划资料
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param o
	 * @return ProjectVO
	 * @roseuid 4861E76F03B9 Edit on: 修改原因：
	 */
	public LayoutVO addLayout(LayoutVO o) {

		super.checkParameterIsNull(o, "common:parameters_null_exception, ");

		if (checkLayoutName(o.getGlebeNo(), o.getProjectId(), null) > 0) {
			throw new BusinessException("house:layout_name_exist_exception",
					BusinessException.WARNING);
		}

		ProjectDomain projectDomain = projectDao.findById(o.getProjectId());
		Layout layout = assembleV2D(o);
		projectDomain.setLayout(layout);
		projectDao.saveOrUpdateObject(projectDomain);
		// 这里调用一个带有查询数据库的方法，是为了强制让数据插入数据库以获取ID
		getDate();
		o.setProjectId(projectDomain.getKeyword());
		o.setKeyword(layout.getKeyword());
		return o;
	}

	/**
	 * 方法描述: 修改期数的建设规划资料
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param o
	 * @return ProjectVO
	 * @roseuid 4861E76F034B Edit on: 修改原因：
	 */
	public LayoutVO editLayout(LayoutVO o) {

		super.checkParameterIsNull(o, "common:parameters_null_exception, ");

		if (checkLayoutName(o.getGlebeNo(), o.getProjectId(), o.getKeyword()) > 0) {
			throw new BusinessException("house:layout_name_exist_exception",
					BusinessException.WARNING);
		}

		ProjectDomain projectDomain = projectDao.findById(o.getProjectId());
		Layout layout = projectDomain.getLayout();
		Layout layoutNew = assembleV2D(o);
		PopulateUtil.populate(layoutNew, layout);

		return o;
	}

	/**
	 * 方法描述:分页某楼盘下查询所有期数
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param o
	 * @param pageNo
	 * @param pageSize
	 * @return Page
	 * @roseuid 4861E770004E
	 */
	public Page queryProjects(ProjectVO o, int pageNo, int pageSize) {

		super.checkParameterIsNull(o, "common:parameters_null_exception, ");
		Long id = o.getAreaId();
		Page page = projectDao.queryPageProjects(id, pageNo, pageSize);
		return page;
	}

	/**
	 * 方法描述:增加某期数下的配套设施
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param EnvironmentVO
	 * @return EnvironmentVO
	 * @roseuid 4861E7700157
	 */
	public EnvironmentVO addEnvironment(EnvironmentVO environmentVO) {

		super.checkParameterIsNull(environmentVO,
				"common:parameters_null_exception, ");

		if (checkEnvironmentName(environmentVO.getName(), environmentVO
				.getProjectId(), null) > 0) {
			throw new BusinessException(
					"house:environment_name_exist_exception",
					BusinessException.WARNING);
		}

		ProjectDomain projectDomain = projectDao.findById(environmentVO
				.getProjectId());

		Environment e = assembleV2D(environmentVO);

		//处理图片
		ImageVO ichnographysImage = environmentVO.getIchnographysImage();
		ImageDomain ichnographysImageDomain = imageService.getPossibleImageDomain(ichnographysImage);
		e.setIchnographysImage(ichnographysImageDomain);

		ImageVO visionImage = environmentVO.getVisionImage();
		ImageDomain visionDomain = imageService.getPossibleImageDomain(visionImage);
		e.setVisionImage(visionDomain);

		projectDomain.getEnvironments().add(e);
		// flush
		projectDao.saveOrUpdateObject(projectDomain);
		// 这里是为了强制让数据插入数据库以获取ID，所以调用一个带有查询数据库的方法
		getDate();
		return assembleD2V(e);
	}

	/**
	 * 方法描述:修改某期数下的配套设施
	 *
	 * @author 胡煦 Create on:2008-7-3
	 * @param EnvironmentVO
	 * @return EnvironmentVO
	 * @roseuid 4861E77001D4
	 */
	public EnvironmentVO editEnvironment(EnvironmentVO environmentVO) {

		super.checkParameterIsNull(environmentVO,
				"common:parameters_null_exception, ");

		if (checkEnvironmentName(environmentVO.getName(), environmentVO
				.getProjectId(), environmentVO.getKeyword()) > 0) {
			throw new BusinessException(
					"house:environment_name_exist_exception",
					BusinessException.WARNING);
		}

		ProjectDomain projectDomain = projectDao.findById(environmentVO
				.getProjectId());

		for (Environment environment : projectDomain.getEnvironments()) {

			if (environmentVO.getKeyword().equals(environment.getKeyword())) {

				PopulateUtil.populate(environmentVO, environment);

				//处理图片
				ImageVO ichnographysImage = environmentVO.getIchnographysImage();
				ImageDomain ichnographysImageDomain = imageService.getPossibleImageDomain(ichnographysImage);
				environment.setIchnographysImage(ichnographysImageDomain);

				ImageVO visionImage = environmentVO.getVisionImage();
				ImageDomain visionDomain = imageService.getPossibleImageDomain(visionImage);
				environment.setVisionImage(visionDomain);

				projectDomain.getEnvironments().add(environment);
				//flush
				projectDao.saveOrUpdateObject(projectDomain);
				// 这里是为了强制让数据插入数据库以获取ID，所以调用一个带有查询数据库的方法
				getDate();
				projectDao.saveOrUpdateObject(projectDomain);
				return assembleD2V(environment);
			}
		}

		return null;
	}

	/**
	 * 功能描述:根据指定期的名字查询记录数<br>
	 * 注：新建判断name的唯一性时，projectId为null；修改判断name的唯一性时，则需要传入projectId<br>
	 *
	 * @param projectName
	 * @param projectId
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkProjectName(String projectName, String projectId) {
		return projectDao.checkProjectName(projectName, projectId);
	}

	/**
	 * 功能描述:根据指定的建设规划的土地宗号查询记录数<br>
	 * 注：新建判断name的唯一性时，layoutId为null；修改判断name的唯一性时，则需要传入layoutId<br>
	 *
	 * @param layoutName
	 * @param projectId
	 * @param layoutId
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkLayoutName(String layoutName, Long projectId, Long layoutId) {
		return projectDao.checkLayoutName(layoutName, projectId, layoutId);
	}

	/**
	 * 功能描述:根据指定配套设施名字查询记录数<br>
	 * 注：新建判断name的唯一性时，environmentId为null；修改判断name的唯一性时，则需要传入environmentId<br>
	 *
	 * @param environmentName
	 * @param projectId
	 * @param environmentId
	 * @return Create author:胡煦 Create on:2008-7-14 Edit author: Edit on: Why:
	 */
	public Long checkEnvironmentName(String environmentName, Long projectId,
			Long environmentId) {
		return projectDao.checkEnvironmentName(environmentName, projectId,
				environmentId);
	}

	/**
	 * @param o
	 * @return ProjectVO
	 * @roseuid 4861E7700242
	 */
	public EnvironmentVO deleteEnvironment(EnvironmentVO environmentVO) {

		super.checkParameterIsNull(environmentVO,
				"common:parameters_null_exception, ");
		ProjectDomain projectDomain = projectDao.findById(environmentVO
				.getProjectId());
		for (Environment environment : projectDomain.getEnvironments()) {
			if (environmentVO.getKeyword().equals(environment.getKeyword())) {

				// 删除平面图
				if (BlankUtil.isBlank(environmentVO.getIchnographysImage())) {
					if (!BlankUtil.isBlank(environment.getIchnographysImage())) {
						imageDao.deleteObject(environment
								.getIchnographysImage());
						// 物理删除图片
						UpLoadFile
								.deleteFile(
										environment.getIchnographysImage() == null ? null
												: environment
														.getIchnographysImage()
														.getPath(),
										environment.getIchnographysImage() == null ? null
												: environment
														.getIchnographysImage()
														.getName());
						environment.setIchnographysImage(null);
					}
				}
				// 删除效果图
				if (BlankUtil.isBlank(environmentVO.getVisionImage())) {
					if (!BlankUtil.isBlank(environment.getVisionImage())) {
						imageDao.deleteObject(environment.getVisionImage());
						// 物理删除图片
						UpLoadFile.deleteFile(
								environment.getVisionImage() == null ? null
										: environment.getVisionImage()
												.getPath(), environment
										.getVisionImage() == null ? null
										: environment.getVisionImage()
												.getName());
						environment.setVisionImage(null);
					}
				}

				projectDomain.getEnvironments().remove(environment);
				return environmentVO;
			}

		}
		return null;
	}

	/**
	 * 方法描述: 添加参建单位
	 *
	 * @author 胡煦 Create on:2008-7-7
	 * @param ProjectVO
	 * @return ProjectVO Edit author: Edit on: 修改原因：
	 */
	public ProjectVO addJoinCorps(ProjectVO projectVO) {
		super.checkParameterIsNull(projectVO,
				"common:parameters_null_exception, ");
		ProjectDomain projectDomain = projectDao.findById(projectVO
				.getKeyword());
		// 清空集合中的值
		projectDomain.getBuildCorps().clear();
		projectDomain.getDesignCorps().clear();
		projectDomain.getDevelopers().clear();
		projectDomain.getSurveillants().clear();
		// 遍历协作伙伴ID，添加关系
		for (Long id : projectVO.getBuildCorps().keySet()) {
			CoWorkerDomain cod = coWorkerDao.findById(id);
			projectDomain.getBuildCorps().add(cod);
		}
		for (Long id : projectVO.getDesignCorps().keySet()) {
			CoWorkerDomain cod = coWorkerDao.findById(id);
			projectDomain.getDesignCorps().add(cod);
		}
		for (Long id : projectVO.getDeveloper().keySet()) {
			CoWorkerDomain cod = coWorkerDao.findById(id);
			projectDomain.getDevelopers().add(cod);
		}
		for (Long id : projectVO.getSurveillants().keySet()) {
			CoWorkerDomain cod = coWorkerDao.findById(id);
			projectDomain.getSurveillants().add(cod);
		}
		return projectVO;
	}

	/**
	 * 方法描述: 删除参建单位，只解除关系
	 *
	 * @author 胡煦 Create on:2008-7-7
	 * @param
	 * @return Edit author: Edit on: 修改原因：
	 */
	public ProjectVO deleteJoinCorps(ProjectVO projectVO, Long coWorkerId) {
		super.checkParameterIsNull(coWorkerId,
				"common:parameters_null_exception, ");
		ProjectDomain projectDomain = projectDao.findById(projectVO
				.getKeyword());
		CoWorkerDomain cod = coWorkerDao.findById(coWorkerId);
		if (cod.getCoworkerType().equals(
				"SysrefcodeDO.CS_CO_WORKER_TYPE_DESIGN")) {
			projectDomain.getDesignCorps().remove(cod);
		}
		if (cod.getCoworkerType().equals(
				"SysrefcodeDO.CS_CO_WORKER_TYPEE_DEVELOPER")) {
			projectDomain.getDevelopers().remove(cod);
		}
		if (cod.getCoworkerType().equals(
				"SysrefcodeDO.CS_CO_WORKER_TYPE_CONSTRUCT")) {
			projectDomain.getBuildCorps().remove(cod);
		}
		if (cod.getCoworkerType().equals(
				"SysrefcodeDO.CS_CO_WORKER_TYPE_SUPERVISE")) {
			projectDomain.getSurveillants().remove(cod);
		}
		return this.assembleD2V(projectDomain);
	}

	/**
	 * 方法描述: 查询方法
	 *
	 * @author 胡煦 Create on:2008-6-27
	 * @param id
	 * @return ProjectVO
	 * @roseuid 4861E77003A9 Edit on: 修改原因：
	 */
	public ProjectVO findProjectById(Long id) {
		super.checkParameterIsNull(id, "common:parameters_null_exception, ");
		return this.assembleD2V(projectDao.findById(id));
	}

	/**
	 *
	 * 功能描述: 根据楼盘id得到期数列表
	 *
	 * @author 付湘辉 Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param areaId
	 * @return List<ProjectVO> 返回结果
	 */
	public List<ProjectVO> getProjectVoListByAreaId(Long areaId) {

		List<ProjectVO> list = new ArrayList<ProjectVO>();

		super.checkParameterIsNull(areaId,
						"common:parameters_null_exception, ");
		ProjectVO projectVO = null;
		List<ProjectDomain> tempList = this.projectDao
				.getProjectDomainListByAreaId(areaId);

		for (ProjectDomain domain : tempList) {
			projectVO = this.assembleD2V(domain);
			list.add(projectVO);
		}

		return list;
	}

	/**
	 * 方法描述: 删除期数并解除其下关系
	 *
	 * @author 胡煦 Create on:2008-7-7
	 * @param
	 * @return Edit author: Edit on: 修改原因：
	 */
	public void deleteProject(Long id) {
		super.checkParameterIsNull(id, "common:parameters_null_exception, ");
		try {
			projectDao.deleteObjectById(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BusinessException("house:house_not_delete_exception");
		}

	}
	/**
	 * 功能描述:  新增或修改一张图片<br>
	 * @param imageVo
	 * @return ImageVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-15  <br>
	 * Edit author: 胡煦 <br>
	 * Edit on:2008-7-25    <br>
	 * Why:  使之适用于本业务   <br>
	 */
	private ImageVO addOrEditImage(ImageVO imageVo){
		ImageVO resultImage = imageVo;
		if (!BlankUtil.isBlank(resultImage)) {
			if (!BlankUtil.isBlank(resultImage.getName())
				&& !BlankUtil.isBlank(resultImage.getPath())) {
				resultImage.setCreator(imageVo.getCreator());
				Long imageId = resultImage.getId();
				if (!BlankUtil.isBlank(imageId)) {//修改操作
					ImageDomain tempImage = this.imageDao.findById(imageId);
					//图片名称不一样，进行修改
					if (!resultImage.getName().equals(tempImage.getName())) {
						UpLoadFile.deleteFile(tempImage.getPath(), tempImage.getName());
						tempImage.setName(imageVo.getName());
						tempImage.setPath(imageVo.getPath());
						imageDao.updateObject(tempImage);
					}
				}else{
					//添加图片
					imageId = this.imageService.addImage(resultImage);

					//得到新增的图片id
					resultImage.setId(imageId);
				}
			}else if(!BlankUtil.isBlank(resultImage.getId())){//只传id，删除文件
				ImageVO tempImageVo = this.imageService.findImageByID(resultImage.getId());
				UpLoadFile.deleteFile(tempImageVo.getPath(), tempImageVo.getName());
				this.imageService.deleteImageByID(tempImageVo.getId());//从数据库删除图片
				resultImage.setId(null);
			}
		}

		return resultImage;
	}
	private IParticipantDao participantDao;

	private IProjectDao projectDao;

	private IAreaDao areaDao;

	private IImageDao imageDao;

	private IImageService imageService;

	private ICoWorkerService coWorkerService;

	private ICoWorkerDao coWorkerDao;

	/**
	 * @param coWorkerDao
	 *            the coWorkerDao to set
	 */
	public void setCoWorkerDao(ICoWorkerDao coWorkerDao) {
		this.coWorkerDao = coWorkerDao;
	}

	/**
	 * @param coWorkerService
	 *            the coWorkerService to set
	 */
	public void setCoWorkerService(ICoWorkerService coWorkerService) {
		this.coWorkerService = coWorkerService;
	}

	/**
	 * @param areaDao
	 *            the areaDao to set
	 */
	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}

	/**
	 * @param projectDao
	 *            the projectDao to set
	 */
	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	/**
	 * @return the projectDao
	 */
	public IProjectDao getProjectDao() {
		return projectDao;
	}

	/**
	 * @param participantDao
	 *            the participantDao to set
	 */
	public void setParticipantDao(IParticipantDao participantDao) {
		this.participantDao = participantDao;
	}

	public CoWorkerVO assembleD2V(CoWorkerDomain c) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @param imageDao
	 *            the imageDao to set
	 */
	public void setImageDao(IImageDao imageDao) {
		this.imageDao = imageDao;
	}

	/**
	 * @param imageService
	 *            the imageService to set
	 */
	public void setImageService(IImageService imageService) {
		this.imageService = imageService;
	}

	public ProjectVO editBuildingOrg(ProjectVO o) {
		// TODO Auto-generated method stub
		return null;
	}

}
